package com.answerz.module_reportissues.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.answerz.module_reportissues.R
import com.answerz.module_reportissues.databinding.ItemOrderStateAttachBinding
import com.answerz.module_reportissues.fragment.OrderDetailFragment
import com.czl.base.util.GlideEngine
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureSelectionConfig
import com.luck.picture.lib.entity.LocalMedia

class OrderStateAttachImageAdapter(val fragment: Fragment):
    RecyclerView.Adapter<BaseDataBindingHolder<ItemOrderStateAttachBinding>>(){

    var urls:ArrayList<LocalMedia> = ArrayList()

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseDataBindingHolder<ItemOrderStateAttachBinding> {
        var view= LayoutInflater.from(parent.context).inflate(R.layout.item_order_state_attach,null)
        return BaseDataBindingHolder<ItemOrderStateAttachBinding>(view)
    }

    override fun onBindViewHolder(
        holder: BaseDataBindingHolder<ItemOrderStateAttachBinding>,
        position: Int
    ) {

        holder.dataBinding!!.url= urls[position]
        holder.dataBinding!!.executePendingBindings()
        holder.dataBinding!!.cvAttachImage.setOnClickListener {

//            val isw= PermissionChecker.checkSelfPermission(fragment.requireContext(),
//                Manifest.permission.WRITE_EXTERNAL_STORAGE)
//            if (isw){
            if (urls.size > 0) {
//                    val pictureType: String = media.mimeType
//                    val mediaType: Int = PictureMimeType.getMimeType(pictureType)
//                    when (mediaType) {
//                        1 ->                             // 预览图片 可自定长按保存路径
                //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
//                        {

                PictureSelectionConfig.getCleanInstance()
                PictureSelector.create(fragment)
                    .themeStyle(R.style.picture_default_style) // xml设置主题
//                        .setPictureStyle(SelectorStyles.getDefaultStyle(fragment.requireContext())) // 动态自定义相册主题
                    //.setPictureWindowAnimationStyle(animationStyle)// 自定义页面启动动画
//                        .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) // 设置相册Activity方向，不设置默认使用系统
                    .isNotPreviewDownload(true) // 预览图片长按是否可以下载
                    //.bindCustomPlayVideoCallback(new MyVideoSelectedPlayCallback(getContext()))// 自定义播放回调控制，用户可以使用自己的视频播放界面
                    .imageEngine(GlideEngine.createGlideEngine()) // 外部传入图片加载引擎，必传项
                    .openExternalPreview(position, urls)
//                        }

//                        2 ->                             // 预览视频
//                            PictureSelector.create(activity)
//                                .externalPictureVideo(media.path)
//                        3 ->                             // 预览音频
//                            PictureSelector.create(activity)
//                                .externalPictureAudio(media.path)
//                    }
            }
//            }else{
//
//            }
        }
    }

    override fun getItemCount(): Int {
        return urls.size ?: 0
    }

}